﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Cloud.Model;
namespace Cloud.DAL
{
    public static class FilesService
    {
        #region SqlCommandText
        private const string Save_CommandText = "insert into files values(@zt,@scwj,@scr,@scrq)";
        private const string Update_CommandText = "update files set zt=@zt,scwj=@scwj,scr=@scr,scrq=@scrq where id=@id";
        private const string Delete_CommandText = "delete from files where id=@id";
        private const string FindAll_CommandText = "select * from files";
        private const string FindById_CommandText = "select * from files where id=@id";
        #endregion


        #region Save
        public static int Save(Files files)
        {
            SqlParameter[] pars = new SqlParameter[] {
                new SqlParameter("@zt",SqlDbType.VarChar),
                new SqlParameter("@scwj",SqlDbType.VarChar),
                new SqlParameter("@scr",SqlDbType.VarChar),
                new SqlParameter("@scrq",SqlDbType.DateTime)
            };
            pars[0].Value = files.Zt;
            pars[1].Value = files.Scwj;
            pars[2].Value = files.Scr;
            pars[3].Value = files.Scrq;

            return DBHelper.ExecuteNonQuery(Save_CommandText, CommandType.Text, pars);
        }
        #endregion

        #region Delete
        public static int Delete(Files files)
        {
            SqlParameter[] pars = new SqlParameter[] { 
                new SqlParameter("@id",SqlDbType.Int)
            };
            pars[0].Value = files.Id;
            return DBHelper.ExecuteNonQuery(Delete_CommandText, CommandType.Text, pars);
        }
        #endregion

        #region Update
        public static int Update(Files files)
        {
            SqlParameter[] pars = new SqlParameter[] 
            {
                new SqlParameter("@zt",SqlDbType.VarChar),
                new SqlParameter("@scwj",SqlDbType.VarChar),
                new SqlParameter("@scr",SqlDbType.VarChar),
                new SqlParameter("@scrq",SqlDbType.DateTime),
                new SqlParameter("@id",SqlDbType.Int)
            };
            pars[0].Value = files.Zt;
            pars[1].Value = files.Scwj;
            pars[2].Value = files.Scr;
            pars[3].Value = files.Scrq;
            pars[4].Value = files.Id;
            return DBHelper.ExecuteNonQuery(Update_CommandText,CommandType.Text,pars);
        }

        #endregion

        #region FindAll
        public static IList<Files> FindAll()
        {
            return CommonQuery(FindAll_CommandText, CommandType.Text, null);
        }
        #endregion

        #region FindById
        public static Files FindById(Int32 id)
        {
            SqlParameter[] pars = new SqlParameter[] 
            {
                new SqlParameter("@id",SqlDbType.Int)
            };
            pars[0].Value = id;
            IList<Files> list = CommonQuery(FindById_CommandText, CommandType.Text, pars);
            return list.Count > 0 ? list[0] : null;

        }
        #endregion

        #region CommonQuery
       
        private static IList<Files> CommonQuery(string commandText,CommandType cmdType,SqlParameter[] pars)
        {
            IList<Files> list=new List<Files>();
            Files files = null;
            using(SqlDataReader reader = DBHelper.ExecuteQuery(commandText,cmdType,pars))
            {
                while(reader.Read())
                {
                    files = new Files();
                    if (reader["id"] == null)
                    {
                        files.Id = 0;
                    }
                    else
                    {
                        files.Id = Convert.ToInt32(reader["id"]);
                    }
                    if (reader["zt"] == null)
                    {
                        files.Zt = "";
                    }
                    else
                    {
                        files.Zt = Convert.ToString(reader["zt"]);
                    }
                    //scwj scr scrq
                    if (reader["scwj"] == null)
                    {
                        files.Scwj = "";
                    }
                    else
                    {
                        files.Scwj = Convert.ToString(reader["scwj"]);
                    }
                    if (reader["scr"] == null)
                    {
                        files.Scr = "";
                    }
                    else
                    {
                        files.Scr = Convert.ToString(reader["scr"]);
                    }
                    if (reader["scrq"] == null)
                    {
                        files.Scrq = new DateTime(1900,01,01,00,00,00);
                    }
                    else
                    {
                        files.Scrq = Convert.ToDateTime(reader["scrq"]);
                    }
                    list.Add(files);
                }
            }
            return list;
            
        }
        #endregion

    }
}
